package com.lanou3g.mynewstest.module.news.comic;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.base.MyBaseFragment;
import com.lanou3g.mynewstest.beans.ComicBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;
import com.lanou3g.mynewstest.tools.StringRequestUtf;

/**
 * Created by dllo on 16/1/26.
 */
public class ComicFragment extends MyBaseFragment {
    private Handler handler;

    private ComicBeans beans;

    private final String HEAD_URL = "http://c.m.163.com/nc/article/list/T1444270454635/";
    private final String FOOT_URL = "-20.html";
    private int times = 0;

    private GridView gridView;
    private ComicGridViewAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_comic, container, false);

        gridView = (GridView) view.findViewById(R.id.comic_rv);
        gridView.setNumColumns(2);
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 101) {
                    beans = (ComicBeans) msg.obj;
                    adapter = new ComicGridViewAdapter(context, beans);
                    gridView.setAdapter(adapter);
                    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                            Intent intent = new Intent(context, ComicIndexActivity.class);
                            intent.putExtra("postId", getBeans().getT1444270454635().get(position).getPostid());
                            context.startActivity(intent);

                        }
                    });
                    gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
                        boolean isShowTop = false;

                        @Override
                        public void onScrollStateChanged(AbsListView view, int scrollState) {

                        }

                        @Override
                        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                            if (firstVisibleItem + visibleItemCount ==  totalItemCount) {
                                if (!isShowTop) {
                                    times++;
                                    getMoreBeans(getUrl(times), beans);
                                    Toast.makeText(context, "正在加载", Toast.LENGTH_SHORT).show();
                                }
                                isShowTop = true;
                            } else {
                                isShowTop = false;

                            }
                        }
                    });
                }
                return false;
            }
        });

        MyRequestQueue.getBeans(ComicBeans.class, getUrl(times), handler);
        return view;
    }

    private String getUrl(int time) {
        String url = HEAD_URL + time * 20 + FOOT_URL;
        return url;
    }

    private void getMoreBeans(String url, final ComicBeans beans) {
        StringRequestUtf requestUtf = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ComicBeans bean = gson.fromJson(response, ComicBeans.class);
                beans.getT1444270454635().addAll(bean.getT1444270454635());
                adapter.addBeans(beans);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        MyRequestQueue.addRequest(requestUtf);
    }

    public ComicBeans getBeans() {
        return beans;
    }
}
